package com.gopay.bis.spdb.expay.impl;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.csii.payment.client.core.MerchantEncryption;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.spdb.expay.operation.HttpTransport;
import com.gopay.bis.spdb.expay.operation.SpdbExPayBuildMessage;
import com.gopay.bis.spdb.expay.operation.SpdbExPayPayParseMessage;
import com.gopay.bis.spdb.expay.operation.SpdbExPayQueryParseMessage;
import com.gopay.bis.spdb.expay.operation.SpdbExPaySignParseMessage;
import com.gopay.bis.spdb.expay.operation.SpdbExPaySignQueryParseMessage;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.spdb.expay.SpdbMessageExPay;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;
import com.gopay.remote.bank.businesses.ExPayService;

/**
 * 浦发快捷支付实现类
 * @SpdbExPayBankServiceImpl.java
 * @author wjq
 * @date 2016/04/12
 */
public class SpdbExPayBankServiceImpl implements ExPayService{

	 private static Log logger = LogFactory.getLog(SpdbExPayBankServiceImpl.class);
	 
	/** HTTP实现类 */
	private ICertHelper spdbExPayCertHelper;
	private HttpTransport spdbHttpTransport;
	private SpdbExPayBuildMessage spdbExPayBuildMessage;
	private SpdbExPaySignParseMessage spdbExPaySignParseMessage;
	private SpdbExPaySignQueryParseMessage spdbExPaySignQueryParseMessage;
	private SpdbExPayPayParseMessage spdbExPayPayParseMessage;
	private SpdbExPayQueryParseMessage spdbExPayQueryParseMessage;
	
	private MerchantEncryption merchantEncryption = new MerchantEncryption();

   
	/**
	 * 快捷支付--签约查询
	 */
    public Object signQuery(BaseMessage baseMsg) throws GopayException {
        try {
            SpdbMessageExPay message = (SpdbMessageExPay)baseMsg;           
            String signMsg = spdbExPayCertHelper.sign(message);
            message.setSignMessage(signMsg);
            // 构建xml
            String buildMsg = spdbExPayBuildMessage.buildSignMessage(message);
            BankLogUtils.printExPaySignSendLog(baseMsg);
            spdbHttpTransport.setUrl(message.getPayUrl());
            spdbHttpTransport.setSendEncoding("GBK");
            Object obj = (Object) this.spdbHttpTransport.submit(buildMsg.getBytes()); // 发送银行
            message = spdbExPaySignQueryParseMessage.parseSpdbMessage(obj,message);// 验签+解析银行返回参数
            BankLogUtils.printExPaySignReturnLog(baseMsg,message);
            return message;
        } catch (Exception e) {
            BankLogUtils.printExPaySignExceptionLog(baseMsg, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6053);
        }
       
    }
    
	public SpdbExPaySignQueryParseMessage getSpdbExPaySignQueryParseMessage() {
        return spdbExPaySignQueryParseMessage;
    }

    public void setSpdbExPaySignQueryParseMessage(SpdbExPaySignQueryParseMessage spdbExPaySignQueryParseMessage) {
        this.spdbExPaySignQueryParseMessage = spdbExPaySignQueryParseMessage;
    }

    /**
	 * 快捷---签约接口
	 */
	public Object certification(BaseMessage baseMsg) throws GopayException {
		try {
			SpdbMessageExPay message = (SpdbMessageExPay)baseMsg;
			//对敏感信息加密
			signSenInfo(message);
			String signMsg = spdbExPayCertHelper.sign(message);
			message.setSignMessage(signMsg);
			// 构建xml
			String buildMsg = spdbExPayBuildMessage.buildSignMessage(message);
			BankLogUtils.printExPaySignSendLog(baseMsg);
			spdbHttpTransport.setUrl(message.getPayUrl());
			spdbHttpTransport.setSendEncoding("GBK");
			Object obj = (Object) this.spdbHttpTransport.submit(buildMsg.getBytes()); // 发送银行
			message = spdbExPaySignParseMessage.parseSpdbMessage(obj);// 验签+解析银行返回参数
			BankLogUtils.printExPaySignReturnLog(baseMsg,message);
			return message;
		} catch (Exception e) {
			BankLogUtils.printExPaySignExceptionLog(baseMsg, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6053);
		}
	}
	

	/**
	 * 快捷---支付接口
	 */
	public Object payment(BaseMessage baseMsg) throws GopayException {
		try {
			SpdbMessageExPay message = (SpdbMessageExPay)baseMsg;
			//对敏感信息加密
			signSenInfo(message);
			String signMsg = spdbExPayCertHelper.sign(message);
			message.setSignMessage(signMsg);
			// 构建xml
			String buildMsg = spdbExPayBuildMessage.buildSignMessage(message);
			BankLogUtils.printExPayPaymentSendLog(baseMsg);
			spdbHttpTransport.setUrl(message.getPayUrl());
			spdbHttpTransport.setSendEncoding("GBK");
			Object obj = (Object) this.spdbHttpTransport.submit(buildMsg.getBytes()); // 发送银行
			message = spdbExPayPayParseMessage.parseSpdbMessage(obj);// 解析银行返回参数
			BankLogUtils.printExPayPaymentReturnLog(baseMsg,message);
			message.setBankRetCode(message.getErrorCode());//add by liuyu, 20170803, 快捷API失败通知
			return message;
		} catch (Exception e) {
			BankLogUtils.printExPaySignExceptionLog(baseMsg, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6057);
		}
	}
	
	/**
	 * 订单查询接口
	 */
	public Object queryOrder(BaseMessage baseMsg) throws GopayException {
		try {
			SpdbMessageExPay message = (SpdbMessageExPay)baseMsg;
			String signMsg = spdbExPayCertHelper.sign(message);
			message.setSignMessage(signMsg);
			// 构建xml
			String buildMsg = spdbExPayBuildMessage.buildSignMessage(message);
			BankLogUtils.printExPayQueryReqLog(baseMsg,buildMsg);
			spdbHttpTransport.setUrl(message.getQueryUrl());
			spdbHttpTransport.setSendEncoding("GBK");
			Object obj = (Object) this.spdbHttpTransport.submit(buildMsg.getBytes()); 
			message = spdbExPayQueryParseMessage.parseSpdbMessageExPay(obj);// 验签+解析银行返回参数
			BankLogUtils.printExPayQueryResLog(message, ToStringBuilder.reflectionToString(message));
			message.setBankRetCode(message.getErrorCode());//add by liuyu, 20170803, 快捷API失败通知
			return message;
		} catch (Exception e) {
			BankLogUtils.printExPayQueryExceptionLog(baseMsg, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6058);
		}
	}
	
	@Override
	public Object sign(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object verify(BaseMessage baseMsg) throws GopayException {
//		return spdbExPayCertHelper.verify(baseMsg);
		return null;
	}

	@Override
	public Object treatyQuery(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object cancel(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	

	@Override
	public Object refund(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public Object sendSms(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object checkSms(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object queryCvn(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}


	public ICertHelper getSpdbExPayCertHelper() {
		return spdbExPayCertHelper;
	}


	public void setSpdbExPayCertHelper(ICertHelper spdbExPayCertHelper) {
		this.spdbExPayCertHelper = spdbExPayCertHelper;
	}


	


	public HttpTransport getSpdbHttpTransport() {
		return spdbHttpTransport;
	}

	public void setSpdbHttpTransport(HttpTransport spdbHttpTransport) {
		this.spdbHttpTransport = spdbHttpTransport;
	}

	public SpdbExPayBuildMessage getSpdbExPayBuildMessage() {
		return spdbExPayBuildMessage;
	}


	public void setSpdbExPayBuildMessage(SpdbExPayBuildMessage spdbExPayBuildMessage) {
		this.spdbExPayBuildMessage = spdbExPayBuildMessage;
	}


	public SpdbExPaySignParseMessage getSpdbExPaySignParseMessage() {
		return spdbExPaySignParseMessage;
	}


	public void setSpdbExPaySignParseMessage(SpdbExPaySignParseMessage spdbExPaySignParseMessage) {
		this.spdbExPaySignParseMessage = spdbExPaySignParseMessage;
	}


	public SpdbExPayPayParseMessage getSpdbExPayPayParseMessage() {
		return spdbExPayPayParseMessage;
	}


	public void setSpdbExPayPayParseMessage(SpdbExPayPayParseMessage spdbExPayPayParseMessage) {
		this.spdbExPayPayParseMessage = spdbExPayPayParseMessage;
	}


	public SpdbExPayQueryParseMessage getSpdbExPayQueryParseMessage() {
		return spdbExPayQueryParseMessage;
	}


	public void setSpdbExPayQueryParseMessage(SpdbExPayQueryParseMessage spdbExPayQueryParseMessage) {
		this.spdbExPayQueryParseMessage = spdbExPayQueryParseMessage;
	}

	/**
	 * 对敏感信息进行加密
	 * @param message
	 */
	private void signSenInfo(SpdbMessageExPay message){
		StringBuilder signText = new StringBuilder();
		signText.append("PayCardName=").append(message.getPayCardName());//姓名
		signText.append("MobileNo=").append(message.getMobileNo());//手机号
		signText.append("Account=").append(message.getAccount());//卡号
		if("U".equals(message.getAccountType())){
			//加密
			signText.append("CVN2=").append(message.getCvv2());//卡号
			
		}
		String encryptData = merchantEncryption.Encryption(signText.toString());
		message.setEncryptData(encryptData);
		
		message.setPlainMessage(message.getPlainMessage() + "|EncryptData=" + encryptData);
	}
	
}
